package com.jeomo.mem.starter.service.impl;

import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.common.core.constant.SearchParam;
import com.jeomo.common.core.constant.Sort;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.masterdata.starter.service.ILevelService;
import com.jeomo.mem.starter.entity.MemberPointsRule;
import com.jeomo.mem.starter.mapper.MemberPointsRuleMapper;
import com.jeomo.mem.starter.service.IMemberPointsRuleService;
import com.jeomo.mem.starter.sysoption.PointsValiditySysOption;
import com.jeomo.mem.starter.util.MemberUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author:Jackpot
 * @create 2020-09-21 17:21
 * @description:
 */
@Service
@Transactional
public class MemberPointsRuleServiceImpl extends BaseServiceImpl<MemberPointsRuleMapper, MemberPointsRule>
        implements IMemberPointsRuleService {

    //查询参数
    private final String USE_LEVEL = "use_level";
    private final String USE_RANGE = "use_range";
    private final String START_FLAG = "start_flag";
    private final String NAME = "name";
    //表字段
    private final String USE_LEVELS = "use_levels";
    private final String CREATE_TIME = "create_time";

    @Resource
    private ILevelService levelService;
    @Resource
    private PointsValiditySysOption pointsValiditySysOption;

    @Override
    public PageResponseDto pageSearch(PageQueryDto dt) {
        PageResponseDto pageResponseDto = super.pageSearch(dt);

        Map<String, String> levelMap = levelService.queryCache();
        List<?> items = pageResponseDto.getItems();
        for (Object item : items) {
            MemberPointsRule memberPointsRule = (MemberPointsRule)item;
            MemberUtil.assignName(memberPointsRule,levelMap);
        }
        return pageResponseDto;
    }

    @Override
    public PageQueryDto initParamAndSort(PageRequest pageRequest){
        PageQueryDto dto = BeanCopyUtil.copyProperties(pageRequest, PageQueryDto.class);
        Map<String, Object> searchParams = dto.getSearchParams();
        if(searchParams!=null){
            Map<String, Object> params = new HashMap<>(8);
            params.put(SearchParam.SEARCH_LIKE+NAME,searchParams.get(NAME));
            params.put(SearchParam.SEARCH_EQ+USE_RANGE,searchParams.get(USE_RANGE));
            params.put(SearchParam.SEARCH_LIKE+USE_LEVELS,searchParams.get(USE_LEVEL));
            params.put(SearchParam.SEARCH_EQ+START_FLAG,searchParams.get(START_FLAG));
            dto.setSearchParams(params);
        }
        Map<String, String> sort = new HashMap<>(8);
        sort.put(CREATE_TIME, Sort.DESC);
        dto.setSorts(sort);
        return dto;
    }

    @Override
    public int updateRuleStatus(String ruleCode,boolean status){

        MemberPointsRule memberPointsRule = new MemberPointsRule();
        memberPointsRule.setCode(ruleCode);
        memberPointsRule.setStartFlag(status);
        boolean result = updateById(memberPointsRule);
        return result==true?1:0;
    }

    @Override
    public LocalDate queryExpiredTime(MemberPointsRule memberPointsRule, LocalDate date) {

        return pointsValiditySysOption.getValidityDate(date);
    }

}
